package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.qi.*;
import com.ysd.lis.mapper.qi.*;
import com.ysd.lis.service.qi.*;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 指标项目条件表 服务实现类
 * </p>
 *
 * @author PRS
 * @since 2024-07-04
 */
@Service
public class QiIndexSqlServiceImpl extends ServiceImpl<QiIndexSqlMapper, QiIndexSql> implements QiIndexSqlService {

    @Autowired
    QiIndexSqlMapper qiIndexSqlMapper;
    @Autowired
    QiIndexSqlTableMapper qiIndexSqlTableMapper;
    @Autowired
    QiIndexSqlTableService qiIndexSqlTableService;

    @Autowired
    QiIndexSqlSelectMapper qiIndexSqlSelectMapper;
    @Autowired
    QiIndexSqlSelectService qiIndexSqlSelectService;

    @Autowired
    QiIndexSqlWhereMapper qiIndexSqlWhereMapper;
    @Autowired
    QiIndexSqlWhereService qiIndexSqlWhereService;

    @Autowired
    QiIndexSqlExistsMapper qiIndexSqlExistsMapper;
    @Autowired
    QiIndexSqlExistsService qiIndexSqlExistsService;
    @Autowired
    QiIndexSqlBusinessMapper qiIndexSqlBusinessMapper;

    @Override
    public Result addQiIndexSql(QiIndexSql qiIndexSql) {
        boolean save = this.save(qiIndexSql);
        if (save) {
            //table
            List<QiIndexSqlTable> qiIndexSqlTableList = qiIndexSql.getQiIndexSqlTableList();
            for(QiIndexSqlTable sqlTable : qiIndexSqlTableList){
                sqlTable.setQiSqlId(qiIndexSql.getId());
            }
            boolean b = qiIndexSqlTableService.saveBatch(qiIndexSqlTableList);

            //SELECT
            List<QiIndexSqlSelect> qiIndexSqlSelectList = qiIndexSql.getQiIndexSqlSelectList();
            for(QiIndexSqlSelect sqlSelect : qiIndexSqlSelectList){
                sqlSelect.setQiSqlId(qiIndexSql.getId());
                if(ToolsUtils.isEmpty(sqlSelect.getQiDatabaseId())){
                    sqlSelect.setQiDatabaseId(qiIndexSql.getQiDatabaseId());
                }
            }
            boolean b1 = qiIndexSqlSelectService.saveBatch(qiIndexSqlSelectList);

            //WHERE
            List<QiIndexSqlWhere> qiIndexSqlWhereList = qiIndexSql.getQiIndexSqlWhereList();
            for(QiIndexSqlWhere sqlWhere : qiIndexSqlWhereList){
                sqlWhere.setQiSqlId(qiIndexSql.getId());
            }
            boolean b2 = qiIndexSqlWhereService.saveBatch(qiIndexSqlWhereList);

            //Specify
            List<QiIndexSqlWhere> qiIndexSqlSpecifyList = qiIndexSql.getQiIndexSqlSpecifyList();
            for(QiIndexSqlWhere sqlSpecify : qiIndexSqlSpecifyList){
                sqlSpecify.setQiSqlId(qiIndexSql.getId());
            }
            boolean b5 = qiIndexSqlWhereService.saveBatch(qiIndexSqlSpecifyList);


            //Exists
            List<QiIndexSqlExists> qiIndexSqlExistsList = qiIndexSql.getQiIndexSqlExistsList();
            for(QiIndexSqlExists sqlExists : qiIndexSqlExistsList){
                sqlExists.setQiSqlId(qiIndexSql.getId());
            }
            boolean b4 = qiIndexSqlExistsService.saveBatch(qiIndexSqlExistsList);

            //ON
            List<QiIndexSqlWhere> qiIndexSqlOnList = new ArrayList<>();
            for(QiIndexSqlTable sqlTable: qiIndexSqlTableList){
                List<QiIndexSqlWhere> onList = sqlTable.getQiIndexSqlOnList();
                if(ToolsUtils.isNotEmpty(onList)){
                    for(QiIndexSqlWhere sqlOn : onList){
                        sqlOn.setQiTableId(sqlTable.getId());
                        sqlOn.setQiSqlId(qiIndexSql.getId());
                        qiIndexSqlOnList.add(sqlOn);
                    }
                }
            }
            boolean b3 = qiIndexSqlWhereService.saveBatch(qiIndexSqlOnList);
            return Result.succ(1, "添加成功", qiIndexSql);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result updateQiIndexSql(QiIndexSql qiIndexSql) {
        boolean save = this.updateById(qiIndexSql);
        if (save) {
            //table
            //先删除 再添加或修改
            LambdaQueryWrapper<QiIndexSqlTable> delTableWrapper = new LambdaQueryWrapper<>();
            delTableWrapper.eq(QiIndexSqlTable::getQiSqlId,qiIndexSql.getId());
            qiIndexSqlTableMapper.delete(delTableWrapper);
            List<QiIndexSqlTable> qiIndexSqlTableList = qiIndexSql.getQiIndexSqlTableList();
            boolean b = qiIndexSqlTableService.saveBatch(qiIndexSqlTableList);

            //SELECT
            //先删除 再添加或修改
            LambdaQueryWrapper<QiIndexSqlSelect> delSelectWrapper = new LambdaQueryWrapper<>();
            delSelectWrapper.eq(QiIndexSqlSelect::getQiSqlId,qiIndexSql.getId());
            qiIndexSqlSelectMapper.delete(delSelectWrapper);
            List<QiIndexSqlSelect> qiIndexSqlSelectList = qiIndexSql.getQiIndexSqlSelectList();
            boolean c = qiIndexSqlSelectService.saveBatch(qiIndexSqlSelectList);


            //WHERE
            //先删除 再添加或修改 同时删除 WHERE ON Specify
            LambdaQueryWrapper<QiIndexSqlWhere> delWhereWrapper = new LambdaQueryWrapper<>();
            delWhereWrapper.eq(QiIndexSqlWhere::getQiSqlId,qiIndexSql.getId());
            qiIndexSqlWhereMapper.delete(delWhereWrapper);
            List<QiIndexSqlWhere> qiIndexSqlWhereList = qiIndexSql.getQiIndexSqlWhereList();
            boolean d = qiIndexSqlWhereService.saveBatch(qiIndexSqlWhereList);

            //Specify
            List<QiIndexSqlWhere> qiIndexSqlSpecifyList = qiIndexSql.getQiIndexSqlSpecifyList();
            boolean dd = qiIndexSqlWhereService.saveBatch(qiIndexSqlSpecifyList);

            //ON
            List<QiIndexSqlWhere> qiIndexSqlOnList = new ArrayList<>();
            for(QiIndexSqlTable sqlTable: qiIndexSqlTableList){
                List<QiIndexSqlWhere> onList = sqlTable.getQiIndexSqlOnList();
                if(ToolsUtils.isNotEmpty(onList)){
                    for(QiIndexSqlWhere sqlOn : onList){
                        sqlOn.setQiTableId(sqlTable.getId());
                        qiIndexSqlOnList.add(sqlOn);
                    }
                }
            }
            boolean b3 = qiIndexSqlWhereService.saveBatch(qiIndexSqlOnList);

            //Exists
            //先删除 再添加或修改
            LambdaQueryWrapper<QiIndexSqlExists> delExistsWrapper = new LambdaQueryWrapper<>();
            delExistsWrapper.eq(QiIndexSqlExists::getQiSqlId,qiIndexSql.getId());
            qiIndexSqlExistsMapper.delete(delExistsWrapper);
            List<QiIndexSqlExists> qiIndexSqlExistsList = qiIndexSql.getQiIndexSqlExistsList();
            boolean b4 = qiIndexSqlExistsService.saveBatch(qiIndexSqlExistsList);

            return Result.succ(1, "编辑成功", qiIndexSql);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @Override
    public Result findQiIndexSqlOne(QiIndexSql pageParam) {
        MPJLambdaWrapper<QiIndexSql> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QiIndexSql.class)
                .eq(QiIndexSql::getIndexCode,pageParam.getIndexCode())
                .eq(QiIndexSql::getItemCode,pageParam.getItemCode())
                .eq(QiIndexSql::getMajorCode,pageParam.getMajorCode())
                .eq(QiIndexSql::getDelFlag,0);
        List<QiIndexSql> qiIndexSqls = qiIndexSqlMapper.selectList(queryWrapper);
        if(ToolsUtils.isNotEmpty(qiIndexSqls)){
            QiIndexSql qiIndexSql = qiIndexSqls.get(0);
            //table
            MPJLambdaWrapper<QiIndexSqlTable> queryTableWrapper = new MPJLambdaWrapper<>();
            queryTableWrapper.selectAll(QiIndexSqlTable.class)
                    //.eq(QiIndexSqlTable::getQiSqlId,qiIndexSql.getId())
                    .eq(QiIndexSqlTable::getIndexCode,pageParam.getIndexCode())
                    .eq(QiIndexSqlTable::getItemCode,pageParam.getItemCode())
                    .eq(QiIndexSqlTable::getMajorCode,pageParam.getMajorCode())
                    .eq(QiIndexSqlTable::getDelFlag,0);
            List<QiIndexSqlTable> qiIndexSqlTables = qiIndexSqlTableMapper.selectList(queryTableWrapper);
            qiIndexSql.setQiIndexSqlTableList(qiIndexSqlTables);
            //SELECT
            MPJLambdaWrapper<QiIndexSqlSelect> querySelectWrapper = new MPJLambdaWrapper<>();
            querySelectWrapper.selectAll(QiIndexSqlSelect.class)
                    //.eq(QiIndexSqlSelect::getQiSqlId,qiIndexSql.getId())
                    .eq(QiIndexSqlSelect::getIndexCode,pageParam.getIndexCode())
                    .eq(QiIndexSqlSelect::getItemCode,pageParam.getItemCode())
                    .eq(QiIndexSqlSelect::getMajorCode,pageParam.getMajorCode())
                    .eq(QiIndexSqlSelect::getDelFlag,0);
            List<QiIndexSqlSelect> qiIndexSqlSelects = qiIndexSqlSelectMapper.selectList(querySelectWrapper);
            qiIndexSql.setQiIndexSqlSelectList(qiIndexSqlSelects);


            MPJLambdaWrapper<QiIndexSqlWhere> queryWhereWrapper = new MPJLambdaWrapper<>();
            queryWhereWrapper.selectAll(QiIndexSqlWhere.class)
                    //.eq(QiIndexSqlWhere::getQiSqlId,qiIndexSql.getId())
                    .eq(QiIndexSqlWhere::getIndexCode,pageParam.getIndexCode())
                    .eq(QiIndexSqlWhere::getItemCode,pageParam.getItemCode())
                    .eq(QiIndexSqlWhere::getMajorCode,pageParam.getMajorCode())
                    .eq(QiIndexSqlWhere::getDelFlag,0);
            List<QiIndexSqlWhere> qiIndexSqlWheres = qiIndexSqlWhereMapper.selectList(queryWhereWrapper);

            //WHERE
            List<QiIndexSqlWhere> whereList = qiIndexSqlWheres.stream().filter(a -> a.getWhereType().equals("WHERE")).collect(Collectors.toList());
            qiIndexSql.setQiIndexSqlWhereList(whereList);

            //Specify
            List<QiIndexSqlWhere> specifyList = qiIndexSqlWheres.stream().filter(a -> a.getWhereType().equals("SPECIFY")).collect(Collectors.toList());
            qiIndexSql.setQiIndexSqlSpecifyList(specifyList);

            //ON
            for(QiIndexSqlTable sqlTable: qiIndexSqlTables){
                List<QiIndexSqlWhere> onList = qiIndexSqlWheres.stream().filter(a -> ToolsUtils.isNotEmpty(a.getQiTableId()) && a.getQiTableId().equals(sqlTable.getId())&&a.getWhereType().equals("ON")).collect(Collectors.toList());
                sqlTable.setQiIndexSqlOnList(onList);
            }

            //Exists
            MPJLambdaWrapper<QiIndexSqlExists> queryExistsWrapper = new MPJLambdaWrapper<>();
            queryExistsWrapper.selectAll(QiIndexSqlExists.class)
                    //.eq(QiIndexSqlExists::getQiSqlId,qiIndexSql.getId())
                    .eq(QiIndexSqlExists::getIndexCode,pageParam.getIndexCode())
                    .eq(QiIndexSqlExists::getItemCode,pageParam.getItemCode())
                    .eq(QiIndexSqlExists::getMajorCode,pageParam.getMajorCode())
                    .eq(QiIndexSqlExists::getDelFlag,0);
            List<QiIndexSqlExists> qiIndexSqlExistss = qiIndexSqlExistsMapper.selectList(queryExistsWrapper);

            qiIndexSql.setQiIndexSqlExistsList(qiIndexSqlExistss);

            return Result.succ(1, "查询成功", qiIndexSql);
        }else {
            return Result.succ(1, "查询成功", null);
        }

    }

    @Override
    public Result deleteQiIndexSql(QiIndexSql qiIndexSql) {
        boolean b = this.removeById(qiIndexSql.getId());
        if(b){
            //删除table
            LambdaUpdateWrapper<QiIndexSqlTable> deleteTableWrapper = new LambdaUpdateWrapper<>();
            deleteTableWrapper.eq(QiIndexSqlTable::getQiSqlId, qiIndexSql.getId());
            qiIndexSqlTableMapper.delete(deleteTableWrapper);
            //删除SELECT
            LambdaUpdateWrapper<QiIndexSqlSelect> deleteSelectWrapper = new LambdaUpdateWrapper<>();
            deleteSelectWrapper.eq(QiIndexSqlSelect::getQiSqlId, qiIndexSql.getId());
            qiIndexSqlSelectMapper.delete(deleteSelectWrapper);

            //删除WHERE
            LambdaUpdateWrapper<QiIndexSqlWhere> deleteWhereWrapper = new LambdaUpdateWrapper<>();
            deleteWhereWrapper.eq(QiIndexSqlWhere::getQiSqlId, qiIndexSql.getId());
            qiIndexSqlWhereMapper.delete(deleteWhereWrapper);

            //删除Exists
            LambdaUpdateWrapper<QiIndexSqlExists> deleteExistsWrapper = new LambdaUpdateWrapper<>();
            deleteExistsWrapper.eq(QiIndexSqlExists::getQiSqlId, qiIndexSql.getId());
            qiIndexSqlExistsMapper.delete(deleteExistsWrapper);

            //删除对照

            LambdaUpdateWrapper<QiIndexSqlBusiness> deleteBusinessWrapper = new LambdaUpdateWrapper<>();
            deleteBusinessWrapper.eq(QiIndexSqlBusiness::getIndexId, qiIndexSql.getIndexId());
            deleteBusinessWrapper.eq(QiIndexSqlBusiness::getItemId, qiIndexSql.getItemId());
            deleteBusinessWrapper.eq(QiIndexSqlBusiness::getMajorId, qiIndexSql.getMajorId());
            qiIndexSqlBusinessMapper.delete(deleteBusinessWrapper);

            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }


    }


}
